﻿using InChatter.Client.WPFClient.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace InChatter.Client.WPFClient.View
{
    /// <summary>
    /// MessagePoolWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MessagePoolWindow : Window
    {
        private MessagePoolWindow()
        {
            InitializeComponent();
            model = new MessagePoolWindowViewModel();
            this.DataContext = model;
            //赋值调用委托方法
            model.MessageShowOrClose = new Action<bool>(ExpandMessageOrClose);
            this.Owner = Application.Current.MainWindow;
        }

        MessagePoolWindowViewModel model;

        #region 单例
        private static MessagePoolWindow _Instance;

        public static MessagePoolWindow Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new MessagePoolWindow();
                }
                return _Instance;
            }
        }

        #endregion

        #region 窗口操作
        /// <summary>
        /// 重写Closing
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }

        /// <summary>
        /// 是否弹出窗口
        /// </summary>
        public void ExpandMessageOrClose(bool IsExpand)
        {
            double ScreenHeight = SystemParameters.WorkArea.BottomRight.Y;
            double ScreenWidth = SystemParameters.WorkArea.BottomRight.X;
            if (this.Visibility != System.Windows.Visibility.Visible)
            {
                this.Top = ScreenHeight - 15;
                this.Left = ScreenWidth - (this.Width + 5);
                this.Show();
                StartAnimation(this.Top, (ScreenHeight - (this.Height + 15)), false);
            }
            else if (!IsExpand)
            {
                StartAnimation(this.Top, ScreenHeight, true);
            }
        }

        /// <summary>
        ///Top动画
        /// </summary>
        /// <param name="thickStart"></param>
        /// <param name="thickEnd"></param>
        /// <param name="visibility"></param>
        private void StartAnimation(double StartPoint, double EndPoint, bool IsClose)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = StartPoint;
            doubleAnimation.To = EndPoint;
            doubleAnimation.Duration = TimeSpan.FromSeconds(0.5);
            doubleAnimation.FillBehavior = FillBehavior.Stop;

            Storyboard.SetTarget(doubleAnimation, this);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(MainWindow.TopProperty));
            Storyboard sb = new Storyboard();
            sb.Children.Add(doubleAnimation);

            sb.Completed += delegate(object sender, EventArgs e)
            {
                if (IsClose)
                {
                    this.Visibility = System.Windows.Visibility.Collapsed;
                }
            };
            sb.Begin();
        }
        #endregion
    }
}
